<?php

class Tree
{
    protected $DOM = null;
    protected $currNode = null;

    function __construct()
    {
        $this->DOM =& new DOMDocument();
        $this->setCurrNode($this->DOM);
    }

    function appendChild($name)
    {
        $child =& $this->DOM->createElement($name);
        return $this->getCurrNode()->appendChild($child);
    }

    function createChild($name)
    {
        return $this->setCurrNode($this->appendChild($name));
    }

    function write($text)
    {
        $child =& $this->DOM->createTextNode($text);
        $this->getCurrNode()->appendChild($child);
    }

    function endChild()
    {
        if ($this->getCurrNode()->parentNode) {
            $this->setCurrNode($this->getCurrNode()->parentNode);
        } else {
            // End of document
        }
    }

    function getDOM()
    {
        return $this->DOM;
    }

    function &setCurrNode(DOMNode &$node)
    {
        return $this->currNode =& $node;
    }

    function &getCurrNode()
    {
        return $this->currNode;
    }

}

?>